import { getSetting } from "@/views/tenantsModule/website/setting";
import {
  getLogo,
  getWebSiteName,
  getCopyRight,
  getInternetContentProvider,
  getStartOnline,
  getIsOpenMsg,
} from "@/utils/cacheUtils/webSiteCache";
import { saveWebSite } from "@/utils/cacheUtils/webSiteCache";

const website = {
  //  存放状态 类似vue data属性
  state: {
    websiteName: "COZE系统",
    logo: "@/assets/logo.png",
    copyright: "Copyright © 2022 COZE管理系统",
    internetContentProvider: "浙 ICP 备 20226666号",
    startOnline: "2022-05-08 12:00:00",
    isOpenMsg: false,
  },
  // 操作state数据的方法的集合，比如对该数据的修改、增加、删除等等，参数一是当前VueX对象中的state，参数二是该方法在被调用时传递参数使用的
  mutations: {
    setWebSiteInfo(state, payLoad) {
      state.websiteName = payLoad.name;
      state.description = payLoad.description;
      state.keyword = payLoad.keyword;
      state.logo = payLoad.logo;
      state.copyright = payLoad.copyright;
      state.internetContentProvider = payLoad.internetContentProvider;
      state.startOnline = payLoad.startOnline;
      state.isOpenMsg = payLoad.isOpenMsg;
    },
  },
  getters: {
    logo(state) {
      return getLogo() || state.websiteName;
    },
    websiteName(state) {
      return getWebSiteName() || state.websiteName;
    },
    copyright(state) {
      return getCopyRight() || state.copyright;
    },
    internetContentProvider(state) {
      return getInternetContentProvider() || state.internetContentProvider;
    },
    startOnline(state) {
      return getStartOnline() || state.startOnline;
    },
    isOpenMsg(state) {
      return getIsOpenMsg() || state.isOpenMsg;
    },
  },
  // 异步操作
  actions: {
    async getWebSitefo(context) {
      return new Promise((resolve, reject) => {
        getSetting()
          .then((res) => {
            //相当于 this.$store.commit,第一个参数是方法名，第二个参数是要传入的数据
            if (res) {
              context.commit("setWebSiteInfo", res.data);
              saveWebSite(res.data);
              resolve(res.data);
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
};

export default website;
